package com.example.datastructure.find;

/**
 * 线性查找（顺序查找）
 */
public class Search {

    /**
     * 在number数组里查找key值得坐标，若没有key值则返回-1
     *
     * @param number
     * @param key
     * @return
     */
    public static int search(int[] number, int key) {

        int result = -1;  //标记数组下标

        //循环遍历数组与key值比较
        for (int i = 0; i < number.length; i++) {
            if (number[i] == key) {
                result = i;
                break;
            }
        }
        if (result == -1) {
            System.out.println("没有" + key);
        }
        return result;
    }

    public static void main(String[] args) {
        int[] number = {12, 24, 53241, 52, 35, 5263, 7};
        int key = 52;
        System.out.println(Search.search(number, key));
    }
}
